package com.panda.web.controller.wechat;

import java.util.List;
import java.util.Arrays;

import com.panda.common.core.domain.R;
import com.panda.common.core.domain.model.WeChatLogin;

import com.panda.web.domain.dto.ReportDTO;

import com.panda.web.domain.vo.CodeVO;


import com.panda.web.domain.vo.UserJob;
import com.panda.web.domain.vo.commons.ScoreVO;
import com.panda.web.entity.aims.CallAi;
import com.panda.web.service.wechat.IWechatUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.panda.web.utils.pageutils.PageResult;

import com.panda.common.annotation.Log;
import com.panda.common.core.controller.BaseController;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.enums.BusinessType;
import com.panda.web.domain.dto.UserDTO;
import com.panda.web.domain.vo.UserVO;


/**
 * 公众号用户Controller
 *
 * @author cpanda
 * @date 2025-02-21
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/wechat")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class WechatUserController extends BaseController {
    private final IWechatUserService userService;


    /**
     * 根据code 获取openId
     */
    @GetMapping("/gzh/code")
    public R login(@RequestParam("code") String code) throws Exception {
        CodeVO vo = userService.getOpenId(code);    // 生成令牌
        return R.ok(vo, "ok");

    }

    /**
     * 根据openId 返回token
     */
    @GetMapping("/gzh/openId")
    public R login(CodeVO vo) throws Exception {
//        AjaxResult ajax = AjaxResult.success();
        WeChatLogin wechatLogin = userService.getToken(vo);    // 生成令牌
        return R.ok(wechatLogin, "ok");

    }


    /**
     * 查询公众号用户列表 下面那个 前面给他加上gzh
     */
    @GetMapping("/list")
    public PageResult<UserVO> List(UserDTO dto,
                                   @RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize) {
        //返回分页信息
        PageResult<UserVO> voList = userService.PageList(dto, pageNum, pageSize);
        return voList;
    }


    /**
     * 查询公众号用户列表 无分页
     */
    @GetMapping("/choiceList")
    public R choiceList(UserDTO dto) {
        List<UserVO> voList = userService.choiceList(dto);
        return R.ok(voList, "ok");
    }

    /**
     * 获取公众号用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userService.selectById(id));
    }

    /**
     * 获取公众号用户详细信息
     */
    @GetMapping(value = "/userinfo/{openid}")
    public AjaxResult getUserInfo(@PathVariable("openid") String openid) {
        return AjaxResult.success(userService.getUserInfo(openid));
    }

    /**
     * 获取公众号用户详细信息
     */
    @GetMapping(value = "/gr/{openid}")
    public AjaxResult getInfo(@PathVariable("openid") String openid) {
        return AjaxResult.success(userService.selectByOpenid(openid));
    }

    /**
     * 新增公众号用户
     */
    @Log(title = "公众号用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserDTO dto) {
        return toAjax(userService.insert(dto));
    }


    /**
     * 修改
     */
    @Log(title = "用户修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody UserDTO dto) {
        return toAjax(userService.updateUser(dto));
    }

    /**
     * 绑定手机号
     */
    @Log(title = "绑定手机号", businessType = BusinessType.UPDATE)
    @PutMapping("/phone")
    public AjaxResult phone(@RequestBody UserDTO dto) {
        return toAjax(userService.phone(dto));
    }

    /**
     * 提交公众号用户信息
     */
    @Log(title = "用户提交", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody UserDTO dto) {
        log.info("Received UserDTO: {}", dto); // 打印日志
        if (dto.getTmObj() == null) {
            log.error("tmObj is null!"); // 检查 tmObj 是否为空
        }
        ScoreVO score = userService.updateUserById(dto);
        return R.ok(score, "ok");
    }

    /**
     * 删除公众号用户
     */
    @Log(title = "公众号用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.removeByUserIds(Arrays.asList(ids)));
    }

    /**
     * 生成报告 接口
     */
    @GetMapping("/viewReport")
    public R getViewReport(ReportDTO dto) throws Exception {
        String path = userService.getViewReport(dto);
        return R.ok(path, "ok");
    }
    /**
     *查询每个人购买的订单
     */
    @GetMapping("/order/{openid}")
    public R getOrderOpenid(@PathVariable String openid){
        List<UserJob> userJobList = userService.getOrderOpenid(openid);
        return R.ok(userJobList,200,"ok");
    }
    /**
     * 获取AI面试报告
     */
    @GetMapping("/user/Aims")
    public R getUserAims( @RequestParam("openid") String openid){
        List<CallAi>callAiList = userService.getUserAimsByOpenid(openid);
        return R.ok(callAiList,200,"ok");

    }
}
